package com.hmdp.config;

import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author BXG
 * @date 3/19/2025 2:11 下午
 * 拦截器配置
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    //  MvcConfig由Sprig构建，可以利用@Resource注入stringRedisTemplate
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    // InterceptorRegistry 是拦截器注册器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                // 不拦截以下,排除不需要拦截的patterns, 根据需求设置
                // /shop/** 放行 /shop开头的所有patterns
                .excludePathPatterns(
                        "/shop/**",
                        "/voucher/**",
                        "/shop-type/**",
                        "/upload/**",
                        "/blog/hot",
                        "/user/code",
                        "/user/login"
                ).order(1);

        // 拦截所有请求,order(0) 比 order(1)优先级高，先执行
        // token刷新的拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);
    }
}
